package DynamicProgramingSeries;

import java.util.Scanner;

public class bagSolution {
    public static void bagSolution_2dim(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int bagWeight = scanner.nextInt();

        int[] weight = new int[n];
        int[] value = new int[n];

        for (int i = 0; i < n; i++) {
            weight[i] = scanner.nextInt();
        }
        for (int i = 0; i < n; i++) {
            value[i] = scanner.nextInt();
        }

        // 初始化DP数组
        int[][] dp = new int[n][bagWeight + 1];
        for (int j = weight[0]; j <= bagWeight; j++) {
            dp[0][j] = value[0];
        }

        for (int i = 1; i < n; i++) {
            for (int j = 0; j <= bagWeight; j++) {
                // 当前背包容量比第i个物品的重量小，放不进去
                if (j < weight[i]) {
                    dp[i][j] = dp[i - 1][j];
                }
                dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
            }
        }
        System.out.println(dp[n - 1][bagWeight]);
    }


    public void bagSolution_1dim(int n, int bagWeight, int[] weight, int[] value){
        /*
        容量为j的背包能装的最大价值
         */
        int[] dp = new int[bagWeight+1];

        // 全部初始化为0
        for(int x : dp) {
            x = 0;
        }

        for(int i=1; i<n; i++) {
            for(int j=bagWeight; j>=weight[i]; j--) {
                dp[j] = Math.max(dp[j], dp[j-weight[i]] + value[i]);
            }
        }

    }
}